# Stack store indices 
# JHL  

# Clean environment and initialize 
rm(list = ls(all = TRUE))

# Packages 
list.of.packages <- c("folderfun", "data.table", "bit64", "lubridate", "foreign", "dplyr", "ggplot2")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages,repos = "http://cran.us.r-project.org")

require(folderfun)
require("data.table")
require("lubridate")
require("parallel")
require("foreign")

# Set paths 
setff("path_home")
path_home <- ffpath_home()

repository <- sprintf('%s/dta/nielsen/PIq_06_15/storePI',path_home)
save_path <- sprintf('%s/raw/PI',path_home)
do_path <- sprintf("%s/do/data_prep/01_price_index",path_home)

# Functions 
PI_functions <- sprintf('%s/05_PIq_fns.R',do_path)
source(PI_functions)

verbose = T 

prev.time <- verbose.Print.Time(verbose, "List files")

# Read csv
files = list.files(repository,pattern=".\\.csv",recursive=T,full.names=T)
files = as.list(files)

# Read data into a list
PI_by_store = lapply(files,fread)

# Bind the list
storePIq_06_15 = rbindlist(PI_by_store, fill=T)

prev.time <- verbose.Print.Time(verbose, "Indices",prev.time)

save(storePIq_06_15,file=sprintf("%s/storePIq_06_15_posted.RData",save_path))

temp = storePIq_06_15[,':='(V1=NULL,Group=NULL)]
write.dta(temp, file=sprintf("storePIq_06_15_posted.dta",save_path))
